package com.dycx.user.converter;

import java.util.Date;

import cn.hutool.core.bean.BeanUtil;
import com.dycx.common.vendor.oss.AliyunOss;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.user.entity.UserInfoDO;
import com.dycx.user.vo.UserInfoVO;
import com.dycx.user.dto.UserInfoDTO;

/**
 * 管理员(UserInfo)实体转换类
 *
 * @author makejava
 * @since 2024-08-19 14:16:40
 */


@Data
public class UserInfoConverter implements Serializable {
    public static UserInfoDO dtoToEntity(UserInfoDTO dto) {
        if (dto == null) return null;
        UserInfoDO entity = BeanUtil.toBean(dto, UserInfoDO.class);
        entity.setAvatar(AliyunOss.delDomain(entity.getAvatar()));
        return entity;
    }

    public static UserInfoVO entityToVo(UserInfoDO entity) {
        if (entity == null) return null;
        UserInfoVO vo = BeanUtil.toBean(entity, UserInfoVO.class);
        vo.setAvatar(AliyunOss.addDomain(vo.getAvatar()));
        return vo;
    }

    public static List<UserInfoVO> entityToVoList(List<UserInfoDO> entityList) {
        List<UserInfoVO> voList = new ArrayList<>();
        for (UserInfoDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<UserInfoVO> entityToVoPage(Page<UserInfoDO> page) {

        List<UserInfoVO> newRecords = new ArrayList<>();

        List<UserInfoDO> records = page.getRecords();
        for (UserInfoDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<UserInfoVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
